﻿#include "DBText.h"
#include "MenuInfo.h"
#include <QByteArray>
#include <QtGlobal>
#include <QMetaType>
#include <QColor>
#include <QString>
#include "db_cfg_g_def.h"

DBText::DBText(void)
{
}

DBText::~DBText(void)
{
}
/*
int DBText::nGetDBText(char *DataBuff, cColumn_Info *pCurColumnInfo, MenuInfo* pMenuInfo, QVariant &ItemData, QString &ItemText, bool &bColorField, QVariant FieldDataBefore)
{
	if (!DataBuff || !pCurColumnInfo || !pMenuInfo ) 
	{
		return -1;
	}
	
	short  DataType = pCurColumnInfo->clmDataType;
	short DisplayType = pCurColumnInfo->clmDisplayType;

	ItemText.clear();
	bColorField = false;

	switch (DataType)
	{
	case DATA_TYPE_CHAR:
	case DATA_TYPE_VARCHAR:
		{
			ItemText = QString::fromLocal8Bit(DataBuff);
			ItemData = QVariant((QString)ItemText);
		}
		break;
	case DATA_TYPE_BYTE:
		{
			char data = 0;
			memcpy(&data, DataBuff, sizeof(char));
			ItemData = QVariant((char)data);
		}
		break;
	case DATA_TYPE_UCHAR:
	case DATA_TYPE_UBYTE:
		{
			unsigned char data = 0;
			memcpy(&data, DataBuff, sizeof(unsigned char));
			ItemData = QVariant((unsigned char)data);
		}
		break;

	case DATA_TYPE_SHORT:
		{
			short data = 0;
			memcpy(&data, DataBuff, sizeof(short));
			ItemData = QVariant((short)data);
		}
		break;
	case DATA_TYPE_USHORT:
		{
			unsigned short data = 0;
			memcpy(&data, DataBuff, sizeof(unsigned short));
			ItemData = QVariant((unsigned short)data);
		}
		break;
	case DATA_TYPE_INT:
		{
			int data = 0;
			memcpy(&data, DataBuff, sizeof(int));
			ItemData = QVariant((int)data);
		}
		break;
	case DATA_TYPE_UINT:
		{
			unsigned int data = 0;
			memcpy(&data, DataBuff, sizeof(unsigned int));
			ItemData = QVariant((unsigned int)data);
		}
		break;
	case DATA_TYPE_FLOAT:
		{
			float data = 0.0;
			memcpy(&data, DataBuff, sizeof(float));
			ItemData = QVariant((float)data);
		}
		break;
	case DATA_TYPE_DOUBLE:
		{
			double data = 0;
			memcpy(&data, DataBuff, sizeof(double));
			ItemData = QVariant((double)data);
		}
		break;

	case DATA_TYPE_LONGLONG:
		{
			qint64 data = 0;
			memcpy(&data, DataBuff, sizeof(qint64));
			ItemData = QVariant((qint64)data);
		}
		break;
	default:
		break;
	}

	switch (DisplayType)
	{
	case DISPLAY_TYPE_CHAR:
	case DISPLAY_TYPE_UCHAR:
	case DISPLAY_TYPE_SHORT:
	case DISPLAY_TYPE_USHORT:
	case DISPLAY_TYPE_INT32:
	case DISPLAY_TYPE_UINT32:
	case DISPLAY_TYPE_LONGLONG:
	case DISPLAY_TYPE_ULONGLONG:
	case DISPLAY_TYPE_INDEXNO_TRACER:
		{
			ItemText.setNum(ItemData.toLongLong());
		}
		break;
	case DISPLAY_TYPE_FLOAT:
		{
			ItemText.setNum((float)ItemData.toDouble());
		}
		break;
	case DISPLAY_TYPE_DOUBLE:
		{
			ItemText.setNum((double)ItemData.toDouble());
		}
		break;
	case DISPLAY_TYPE_STRING:
	case DISPLAY_TYPE_EXP:
		{
			ItemText = ItemData.toString();
		}
		break;
	case DISPLAY_TYPE_BOOLEN:
		{
			if (SYS_STATE_VALID == ItemData.toInt())
			{
				ItemText = QString::fromLocal8Bit("是");
			}else
			{
				ItemText = QString::fromLocal8Bit("否");
			}
		}
		break;
	case DISPLAY_TYPE_MENU:
		{
			if (QString::fromLocal8Bit(pCurColumnInfo->ColumnEngName)=="m_ProtType")
			{
				QString MenuName=pMenuInfo->GetMenuNameByTaskType(FieldDataBefore.toInt());
				if (MenuName!="")
				{
					pMenuInfo->GetMenuShowTextByMenuName(MenuName.toLocal8Bit().data(),ItemData.toInt(), ItemText);
				}
			}
			else
			{
				pMenuInfo->GetMenuShowText(pCurColumnInfo->ColumnId,ItemData.toInt(), ItemText);
			}
		}
		break;
	case DISPLAY_TYPE_MULTI_MENU:
//	case DISPLAY_TYPE_NODE_CFG_STATE:
		{
			pMenuInfo->GetMultiMenuText(pCurColumnInfo->ColumnId, ItemData.toInt(), ItemText);
		}
		break;
	case DISPLAY_TYPE_DATE:
		{
			ItemText.setNum(ItemData.toInt());
		}
		break;
	case DISPLAY_TYPE_TIME:
		{
			ItemText.setNum(ItemData.toInt());
		}
		break;
	case DISPLAY_TYPE_DATETIME:
	case DISPLAY_TYPE_TOTAL_TIME:
		{
			QDateTime dateTime ;
			dateTime.setTime_t(ItemData.toUInt());
			ItemText = dateTime.toString(Qt::ISODate);
		}
		break;
	case DISPLAY_TYPE_RELATED:
		{
			GetRelateShowText(pCurColumnInfo->clmRltAppType, pCurColumnInfo->clmRltTableId, pCurColumnInfo->clmRltColEngName,pCurColumnInfo->clmRltRSColEngName, ItemData.toInt(), ItemText);
			//pMenuInfo->GetRelateMenuShowText(pCurColumnInfo->clmRltAppType, pCurColumnInfo->clmRltTableId, pCurColumnInfo->clmRltColEngName, ItemData.toInt(), ItemText);
		}
		break;
	case DIPPLAY_TYPE_COLOR:
		{
			QColor color( QRgb(ItemData.toUInt()) );
			ItemText = QString("(%1, %2, %3 ,%4)").arg(color.red()).arg(color.green()).arg(color.blue()).arg(color.alpha());
			bColorField = true;
		}
		break;
	case DISPLAY_TYPE_PASSWORD:
		{
			ItemText.setNum(ItemData.toInt());
		}
		break;
	case DISPLAY_TYPE_HEX:
		{
			ItemText.setNum(ItemData.toInt(), 16);
		}
		break;
	case DISPLAY_TYPE_TABLE:
		{
			int tableId = ItemData.toInt();		
			g_DbCfgFunObj.GetTableChnNamebyAppType_TableId(SYSTEM_TYPE_NSA_YD, tableId, ItemText);
		}
		break;
	case DISPLAY_TYPE_IP_CFG:	
		{
			quint32 IntVal= ItemData.toInt();
			ItemText = QString::fromLocal8Bit("%1.%2.%3.%4")
				.arg(IntVal/0x1000000)
				.arg(IntVal%0x1000000/0x10000)
				.arg(IntVal%0x10000/0x100)
				.arg(IntVal%0x100);
		}
		break;
	case DISPLAY_TYPE_COLUMN:
		return 0;
		break;
	case DISPLAY_TYPE_INDEXNO:
		{
			return 0;
		}
		break;
	default:
		break;
	}

	return 1;

}
*/
int DBText::nGetDBText(char *DataBuff, char *RltBuff, cColumn_Info *pCurColumnInfo, MenuInfo* pMenuInfo, QVariant &ItemData,  QString &ItemText, bool &bColorField/*, QVariant FieldDataBefore*/)
{
	if ( !DataBuff || !pCurColumnInfo || !pMenuInfo /*|| DataOffset<0  || buffLen<=0*/) 
	{
		return -1;
	}
// 	if (DataOffset >= buffLen)
// 	{
// 		return -2;
// 	}
	short  DataType = pCurColumnInfo->clmDataType;
	short DisplayType = pCurColumnInfo->clmDisplayType;

	ItemText.clear();
	bColorField = false;

	switch (DataType)
	{
	case DATA_TYPE_CHAR:
	case DATA_TYPE_VARCHAR:
		{
			//ItemText = QString::fromLocal8Bit(RecordBuff+DataOffset);
			ItemText = QString::fromLocal8Bit(DataBuff);
			ItemData = QVariant((QString)ItemText);
		}
		break;
	case DATA_TYPE_BYTE:
		{
			char data = 0;
			//memcpy(&data, RecordBuff+DataOffset, sizeof(char));
			memcpy(&data, DataBuff, sizeof(char));
			ItemData = QVariant((char)data);
		}
		break;
	case DATA_TYPE_UCHAR:
	case DATA_TYPE_UBYTE:
		{
			unsigned char data = 0;
			//memcpy(&data, RecordBuff+DataOffset, sizeof(unsigned char));
			memcpy(&data, DataBuff, sizeof(unsigned char));
			ItemData = QVariant((unsigned char)data);
		}
		break;

	case DATA_TYPE_SHORT:
		{
			short data = 0;
			//memcpy(&data, RecordBuff+DataOffset, sizeof(short));
			memcpy(&data, DataBuff, sizeof(short));
			ItemData = QVariant((short)data);
		}
		break;
	case DATA_TYPE_USHORT:
		{
			unsigned short data = 0;
			//memcpy(&data, RecordBuff+DataOffset, sizeof(unsigned short));
			memcpy(&data, DataBuff, sizeof(unsigned short));
			ItemData = QVariant((unsigned short)data);
		}
		break;
	case DATA_TYPE_INT:
		{
			int data = 0;
			//memcpy(&data, RecordBuff+DataOffset, sizeof(int));
			memcpy(&data, DataBuff, sizeof(int));
			ItemData = QVariant((int)data);
		}
		break;
	case DATA_TYPE_UINT:
		{
			unsigned int data = 0;
			//memcpy(&data, RecordBuff+DataOffset, sizeof(unsigned int));
			memcpy(&data, DataBuff, sizeof(unsigned int));
			ItemData = QVariant((unsigned int)data);
		}
		break;
	case DATA_TYPE_FLOAT:
		{
			float data = 0.0;
			//memcpy(&data, RecordBuff+DataOffset, sizeof(float));
			memcpy(&data, DataBuff, sizeof(float));
			ItemData = QVariant((float)data);
		}
		break;
	case DATA_TYPE_DOUBLE:
		{
			double data = 0;
			//memcpy(&data, RecordBuff+DataOffset, sizeof(double));
			memcpy(&data, DataBuff, sizeof(double));
			ItemData = QVariant((double)data);
		}
		break;

	case DATA_TYPE_LONGLONG:
		{
			qint64 data = 0;
			//memcpy(&data, RecordBuff+DataOffset, sizeof(qint64));
			memcpy(&data, DataBuff, sizeof(qint64));
			ItemData = QVariant((qint64)data);
		}
		break;
	default:
		break;
	}

	switch (DisplayType)
	{
	case DISPLAY_TYPE_CHAR:
	case DISPLAY_TYPE_UCHAR:
	case DISPLAY_TYPE_SHORT:
	case DISPLAY_TYPE_USHORT:
	case DISPLAY_TYPE_INT32:
	case DISPLAY_TYPE_UINT32:
	case DISPLAY_TYPE_LONGLONG:
	case DISPLAY_TYPE_ULONGLONG:

		{
			ItemText.setNum(ItemData.toLongLong());
		}
		break;
	case DISPLAY_TYPE_FLOAT:
		{
             ItemText.setNum((float)ItemData.toDouble());
		}
		break;
	case DISPLAY_TYPE_DOUBLE:
		{
			ItemText.setNum((double)ItemData.toDouble());
		}
		break;
	case DISPLAY_TYPE_STRING:
	case DISPLAY_TYPE_EXP:
		{
			ItemText = ItemData.toString();
		}
		break;
	case DISPLAY_TYPE_BOOLEN:
		{
			if (SYS_STATE_VALID == ItemData.toInt())
			{
				ItemText = QString::fromLocal8Bit("是");
			}else
			{
				ItemText = QString::fromLocal8Bit("否");
			}
		}
		break;
	case DISPLAY_TYPE_MENU:
		{
			if (QString::fromLocal8Bit(pCurColumnInfo->ColumnEngName)=="m_ProtType")
			{  
				unsigned int taskUsage=0;
				if (RltBuff)
				{
					memcpy(&taskUsage, RltBuff, sizeof(unsigned int));
				}
			
				QString MenuName=pMenuInfo->GetMenuNameByTaskType(taskUsage);
				if (MenuName!="")
				{
					pMenuInfo->GetMenuShowTextByMenuName(MenuName.toLocal8Bit().data(),ItemData.toInt(), ItemText);
				}
			}
			else
			{
				pMenuInfo->GetMenuShowText(pCurColumnInfo->ColumnId,ItemData.toInt(), ItemText);
			}
		}
		break;
	case DISPLAY_TYPE_MULTI_MENU:
//	case DISPLAY_TYPE_NODE_CFG_STATE:
		{
			pMenuInfo->GetMultiMenuText(pCurColumnInfo->ColumnId, ItemData.toInt(), ItemText);
		}
		break;
	case DISPLAY_TYPE_DATE:
		{
			ItemText.setNum(ItemData.toInt());
		}
		break;
	case DISPLAY_TYPE_TIME:
		{
			ItemText.setNum(ItemData.toInt());
		}
		break;
	case DISPLAY_TYPE_DATETIME:
	case DISPLAY_TYPE_TOTAL_TIME:
		{
			QDateTime dateTime ;
			dateTime.setTime_t(ItemData.toUInt());
			ItemText = dateTime.toString(Qt::ISODate);
		}
		break;
	case DISPLAY_TYPE_RELATED:
		{
			GetRelateShowText(pCurColumnInfo->clmRltAppType, pCurColumnInfo->clmRltTableId, pCurColumnInfo->clmRltColEngName,pCurColumnInfo->clmRltRSColEngName, ItemData.toInt(), ItemText);
			//pMenuInfo->GetRelateMenuShowText(pCurColumnInfo->clmRltAppType, pCurColumnInfo->clmRltTableId, pCurColumnInfo->clmRltColEngName, ItemData.toInt(), ItemText);
		}
		break;
	case DIPPLAY_TYPE_COLOR:
		{
			QColor color( QRgb(ItemData.toUInt()) );
			ItemText = QString("(%1, %2, %3 ,%4)").arg(color.red()).arg(color.green()).arg(color.blue()).arg(color.alpha());
			bColorField = true;
		}
		break;
	case DISPLAY_TYPE_PASSWORD:
		{
			ItemText.setNum(ItemData.toInt());
		}
		break;
	case DISPLAY_TYPE_HEX:
		{
			ItemText.setNum(ItemData.toInt(), 16);
			ItemText = ItemText.toUpper();
		}
		break;
	case DISPLAY_TYPE_IP_CFG:	
		{
			quint32 IntVal= ItemData.toInt();
			ItemText = QString::fromLocal8Bit("%1.%2.%3.%4")
				.arg(IntVal/0x1000000)
				.arg(IntVal%0x1000000/0x10000)
				.arg(IntVal%0x10000/0x100)
				.arg(IntVal%0x100);
		}
		break;
	case DISPLAY_TYPE_TABLE:
		{
			int tableId = ItemData.toInt();			
			g_DbCfgFunObj.GetTableChnNamebyAppType_TableId(SYSTEM_TYPE_NSA_YD, tableId, ItemText);
		}
		break;
	case DISPLAY_TYPE_COLUMN:
		{
			/*if (TABLE_REXPDEFINE_INFO == pCurColumnInfo->TableId)
			{
				g_DbCfgFunObj.GetColChnNamebyAppType_TableId_ColId(SYSTEM_TYPE_NSA_RD, ((rExpDefine_Info*)RecordBuff)->expOutTableId, ((rExpDefine_Info*)RecordBuff)->expOutColumnId, ItemText);	
			}
			else if (TABLE_REXPVARIABLE_LIST == pCurColumnInfo->TableId)
			{
				g_DbCfgFunObj.GetColChnNamebyAppType_TableId_ColId(SYSTEM_TYPE_NSA_RD, ((rExpVariable_List*)RecordBuff)->TableId, ((rExpVariable_List*)RecordBuff)->ColumnId, ItemText);
			}else if (TABLE_RTRACER_CAUSE_BUFF == pCurColumnInfo->TableId)
			{
				g_DbCfgFunObj.GetColChnNamebyAppType_TableId_ColId(SYSTEM_TYPE_NSA_RD, ((rTracer_Cause_Buff*)RecordBuff)->TableId, ((rTracer_Cause_Buff*)RecordBuff)->ColumnId, ItemText);	
			}
			else if (TABLE_RTRACER_POINT_BUFF == pCurColumnInfo->TableId)
			{
				g_DbCfgFunObj.GetColChnNamebyAppType_TableId_ColId(SYSTEM_TYPE_NSA_RD, ((rTracer_Point_Buff*)RecordBuff)->TableId, ((rTracer_Point_Buff*)RecordBuff)->ColumnId, ItemText);	
			}*/
			
		}
		break;
	case DISPLAY_TYPE_INDEXNO:
		{
			QString recname,outioname;
			unsigned int devparentid;
			int outID = ItemData.toInt();	
			int tableid = outID/PARENT_OFFSET;
			g_DbCfgFunObj.GetParentIdForSIYAO(tableid,outID%PARENT_OFFSET,devparentid);

			g_DbCfgFunObj.GetRecordNamebyAppType_TableId_RecordId(SYSTEM_TYPE_NSA_YD,TABLE_RDEVS_INFO,devparentid%PARENT_OFFSET,recname);
			outioname = recname;
			g_DbCfgFunObj.GetRecordNamebyAppType_TableId_RecordId(SYSTEM_TYPE_NSA_YD,tableid,outID%PARENT_OFFSET,recname);
			outioname = outioname +"  :  "+recname;
            ItemText = outioname;
		}
		break;
	default:
		return 0;
		break;
	}
	
	return 1;
}


//	获取域显示文字
int DBText::nGetDataBuff(char *&DataBuff, int *Buff_len, cColumn_Info *pColumnInfo, QVariant ItemData/*, QString ItemText*/)
{
	if (DataBuff != NULL || !Buff_len || !pColumnInfo )
	{
		return -1;
	}

	if (pColumnInfo->clmDataLength>0 && pColumnInfo->clmDataType != DATA_TYPE_VARCHAR)
	{
		DataBuff = new char[pColumnInfo->clmDataLength];
		memset(DataBuff, 0, pColumnInfo->clmDataLength);
	}

	if (!DataBuff)
	{
		return -2;
	}
	*Buff_len = pColumnInfo->clmDataLength;
	switch (ItemData.type())
	{
	case QVariant::Int:
		{
			switch (pColumnInfo->clmDataType)
			{
			case DATA_TYPE_BYTE:
				{
					char	DataValue = ItemData.toInt();
					memcpy(DataBuff, (char*)&DataValue, sizeof(char));
				}
				break;
			case DATA_TYPE_UCHAR:
			case DATA_TYPE_UBYTE:
				{	
					unsigned char	DataValue = ItemData.toInt();
					memcpy(DataBuff, (char*)&DataValue, sizeof(unsigned char));
				}
				break;
			case DATA_TYPE_SHORT:
				{
					short	DataValue = ItemData.toInt();
					memcpy(DataBuff, (char*)&DataValue, sizeof(short));
				}
				break;
			case DATA_TYPE_USHORT:
				{
					unsigned short	DataValue = ItemData.toInt();
					memcpy(DataBuff, (char*)&DataValue, sizeof(unsigned short));
				}
				break;
			case DATA_TYPE_UINT:
				{
					unsigned int DataValue = ItemData.toInt();
					memcpy(DataBuff, (char*)&DataValue, sizeof(unsigned int));
				}
				break;
			case DATA_TYPE_INT:
			default:
				{
					int DataValue = ItemData.toInt();
					memcpy(DataBuff, (char*)&DataValue, sizeof(int));
				}
				break;
			}
		}
		break;
	case QVariant::UInt:
		{
			unsigned int	DataValue = 0;
			DataValue = ItemData.toUInt();
			memcpy(DataBuff, (char*)&DataValue, sizeof(unsigned int));
		}
		break;
	case QVariant::LongLong:
		{
			qint64	DataValue = 0;
			DataValue = ItemData.toLongLong();
			memcpy(DataBuff, (char*)&DataValue, sizeof(qint64));
		}
		break;
	case QVariant::Double:
		{
			if (DATA_TYPE_FLOAT == pColumnInfo->clmDataType)
			{
                float	DataValue = ItemData.toDouble();
				memcpy(DataBuff, (char*)&DataValue, sizeof(float));
			}else
			{
				double DataValue = ItemData.toDouble();
				memcpy(DataBuff, (char*)&DataValue, sizeof(double));
			}
		}
		break;
	case QMetaType::Float:
		{
			if (DATA_TYPE_FLOAT == pColumnInfo->clmDataType)
			{
                float	DataValue = ItemData.toDouble();
				memcpy(DataBuff, (char*)&DataValue, sizeof(float));
			}else
			{
				double DataValue = ItemData.toDouble();
				memcpy(DataBuff, (char*)&DataValue, sizeof(double));
			}
		}
		break;
	case QVariant::String:
		{
			QByteArray TextData = ItemData.toString().toLocal8Bit();

			short  length = TextData.length();
			short	clmlen = pColumnInfo->clmDataLength;
			length = qMin(length, clmlen);
			memcpy(DataBuff, TextData.data(), length);
		}
		break;
	default:
		break;
	}
	
	return 1;
}


// 通过域信息和实际值得到关联菜单显示文字
int DBText::GetRelateShowText(int RltAppType, int RltTableId, char *pRltColName, char *pShowColName, int FieldValue, QString &ItemText)
{
	ItemText.clear();
	if (RltAppType <= 0 || !pRltColName || !pShowColName)
		return -1;

	unsigned int AppType_show = RltAppType;
	unsigned int TableId_show = 0;
	unsigned int RltValue = 0;	
	TableId_show = FieldValue/PARENT_OFFSET;
	RltValue = FieldValue%PARENT_OFFSET;
	if (TABLE_RPARAMS_TERMINAL == TableId_show)
	{
		ItemText = QString::fromLocal8Bit("终端");
		return 1;
	}
	if (!(TableId_show>0 && TableId_show != RltTableId))
	{
		TableId_show = RltTableId;
	}
	if (RltValue<=0)
	{
		return -2;
	}
	char TableName[TABLE_ENG_NAME_LEN];
	memset(TableName, 0, TABLE_ENG_NAME_LEN);
	int Ret= g_DbCfgFunObj.GetTableEngNamebyAppType_TableId(AppType_show, TableId_show, TableName);
	if (Ret<=0)
	{
		return -3;
	}
	
	char	sql[SQL_MAX_LEN];
	char	*buff=NULL;
	int	buff_len=0;

	char	*pEngName = NULL;
	int	Result = 0;
	int	RltShowColLen = 0;

	memset(sql, 0, SQL_MAX_LEN);
	if (strlen(pRltColName) > 0 && 
		strlen(pShowColName) > 0 )
	{
		sprintf(sql, "select %s from %s where %s=%d", pShowColName,TableName, pRltColName, RltValue);
	}else if (strlen(pRltColName) > 0 )
	{
		sprintf(sql, "select Name from %s where %s=%d", TableName, pRltColName, RltValue);
	}else if (strlen(pShowColName) > 0 )
	{
		sprintf(sql, "select %s from %s where ID=%d", pShowColName, TableName, RltValue);
	}else
	{
		sprintf(sql, "select Name from %s where ID=%d", TableName, RltValue);
	}
// 	Result = g_DbCfgFunObj.GetColLenbyAppType_TableId_ColEnName(AppType_show, TableId_show, pRltColName, RltColLen);
// 	if (RltColLen<=0)
// 	{
// 		RltColLen = sizeof(XUINT);
// 	}
	Result = g_DbCfgFunObj.GetColLenbyAppType_TableId_ColEnName(AppType_show, TableId_show, pShowColName, RltShowColLen);
	if (RltShowColLen <= 0)
	{
		RltShowColLen = STRING_NORMAL_LEN;
	}

	Result = g_pSqLiteDB.DB_SelectString(RltShowColLen, sql, buff, &buff_len);
	if( Result >0 && buff && buff_len>0)
	{
		ItemText = QString::fromLocal8Bit(buff);
	}
	if (buff)
	{
		delete []buff;
		buff=NULL;
	}
	
	return Result;
}

/*
int DBText::GetMenuText(int nFieldIndex,int nMenuVal,char *&szText,CMenuInfo *pMenuInfo,COLUMN_INFO *pColumnINfo,int nFlag)
{

	int index;
	if(nFlag == 2)
	{
		char MainName[40],SubName[40];
		if(pMenuInfo->GetDoubleItemText(nFieldIndex,nMenuVal,MainName,SubName)<0)
			return -1;

		//加5是::的长度.目的:有更充足的空间
		int nStrLen = sizeof(MainName)+sizeof(SubName)+5;
		if(nStrLen > FIELD_TEXT_LEN)
		{
			szText = (char*)realloc(szText,nStrLen*sizeof(char));
			memset(szText,0,sizeof(szText));
		}
		sprintf(szText,"%s :: %s",MainName,SubName);

	}
	else if((index=pMenuInfo->GetMenu(pColumnINfo[nFieldIndex].column_id,nMenuVal,szText))<0)
		strcpy(szText,SELECT_MENU);

	return index;
}
//得到时间的字符串
INT CDBText::GetTimeText(time_t nTime,char *szTime,int nTimeType)
{
	if(nTime <=0)
	{
		szTime[0]='\0';
		return -1;
	}
	struct tm stm;
	struct tm *ptm;
	ptm = &stm;
	ptm = localtime(&nTime);
	if(!ptm) 
	{
		szTime[0]='\0';
		return -1;
	}

	if(nTimeType ==D_DATE_TYPE)//D_DATE_TYPE
		sprintf(szTime,"%4d年%02d月%02d日",ptm->tm_year+1900,ptm->tm_mon+1,ptm->tm_mday);
	else if(nTimeType == D_TIME_TYPE)//D_TIME_TYPE
		sprintf(szTime,"%2d时%02d分%02d秒",ptm->tm_hour,ptm->tm_min,ptm->tm_sec);
	else if(nTimeType == D_DATETIME_TYPE)//D_DATETIME_TYPE
		sprintf(szTime,"%4d年%02d月%02d日 %02d时%02d分%02d秒",
		ptm->tm_year+1900,ptm->tm_mon+1,ptm->tm_mday,
		ptm->tm_hour,ptm->tm_min,ptm->tm_sec);
	else if (nTimeType==D_TOTAL_TIME_TYPE)
		sprintf(szTime,"%d日%02d时%02d分%02d秒",nTime/86400,ptm->tm_hour,ptm->tm_min,ptm->tm_sec);

	return 1;
}

//得到引用显示的内容
INT CDBText::GetRefText(char *szFieldBuf,int nFieldIndex,char *&szText)
{	

	if(!szFieldBuf) 
	{
		szText[0]='\0';
		return -1;
	}
	char *szRefText = 0;

	int nStrLen = strlen(szRefText);
	if(nStrLen > FIELD_TEXT_LEN)
	{
		szText = (char*)realloc(szText,nStrLen*sizeof(char));
		memset(szText,0,sizeof(szText));
	}
	strcpy(szText,szRefText);

	free(szRefText);
	return 1;
}
//解释KEYID ***暂时没用***
INT CDBText::GetKEYIDText(char *szFieldBuf,int nFieldIndex,char *&szText)
{
	return 1;
}
INT CDBText::GetChoiceText(CNSA3000DB_Interface *pDataBase,int nTableID,char *buff,int nFieldIndex,char *&szText,COLUMN_INFO *pColumnInfo)
{
	int nID=0;
	char szTableName[40];
	memset(szTableName,0,40);

	memcpy(&nID,buff,sizeof(int));
	if(nID<=0)
	{
		sprintf(szText,"无");
		return -1;
	}
	switch(nTableID) //需要重新定位
	{
	case TABLE_RYXDEFINE_INFO:
		{
			if(strcmp(pColumnInfo[nFieldIndex].column_name_eng,"YKIndex")==0)
			{
				sprintf(szTableName,"YK_DEFINE_TABLE");
			}
			if(strcmp(pColumnInfo[nFieldIndex].column_name_eng,"DbPointID")==0)
			{
				sprintf(szTableName,"YX_DEFINE_TABLE");
			}
		}
		break;
	case TABLE_RYCDEFINE_INFO:
		{
			if(strcmp(pColumnInfo[nFieldIndex].column_name_eng,"YXToZero")==0)
			{
				sprintf(szTableName,"YX_DEFINE_TABLE");
			}else if (strcmp(pColumnInfo[nFieldIndex].column_name_eng,"SaltantYx")==0)		
			{
				sprintf(szTableName,"YX_DEFINE_TABLE");
			}
		}
		break;
	case ID_TABLE_YK_DEFINE_TABLE:
		{
			if(strcmp(pColumnInfo[nFieldIndex].column_name_eng,"YXIndex")==0 ||
				strcmp(pColumnInfo[nFieldIndex].column_name_eng,"CloseLogic")==0 ||			
				strcmp(pColumnInfo[nFieldIndex].column_name_eng,"Bak_Int0")==0)				
			{
				sprintf(szTableName,"YX_DEFINE_TABLE");
			}
		}
		break;
	case ID_TABLE_YT_DEFINE_TABLE:
		{
			if(strcmp(pColumnInfo[nFieldIndex].column_name_eng,"YCNum")==0)
			{
				sprintf(szTableName,"YC_DEFINE_TABLE");
				break;
			}
			if(strcmp(pColumnInfo[nFieldIndex].column_name_eng,"TenNum")==0)
			{
				sprintf(szTableName,"YX_DEFINE_TABLE");
				break;
			}
			CString str;
			str.Format("%s",pColumnInfo[nFieldIndex].column_name_eng);
			if(str.Find("DWNum")>=0)
			{
				sprintf(szTableName,"YX_DEFINE_TABLE");
				break;
			}
			if(str.Find("YKObject")>=0)
			{
				sprintf(szTableName,"YK_DEFINE_TABLE");
				break;
			}
		}
		break;
	case ID_TABLE_SYSTEM_INFO:
		{
			sprintf(szTableName,"YX_DEFINE_TABLE");
		}
		break;
	case ID_TABLE_TRANSMIT_YK:
		{
			sprintf(szTableName, "YX_DEFINE_TABLE");
		}
		break;
	}
	if(pDataBase)
	{
		char sql[MAX_SQL_LENGTH];
		char *sqlbuff=NULL;
		int buff_len=0;
		memset(sql,0,MAX_SQL_LENGTH);
		sprintf(sql,"select Name from %s where ID=%d",szTableName,nID);
		if(pDataBase->DB_Select(m_Sys_DB.GetActiveNSA3000N_DB(),sql,sqlbuff,&buff_len)>0)
		{
			sprintf(szText,sqlbuff);
			delete sqlbuff;
			sqlbuff=NULL;
			return 1;
		}
	}
	
	return 0;
}

//获取互为备用的显示信息
INT CDBText::GetCongenerText(CNSA3000DB_Interface *pDataBase,char *buff,INT nTableID,char *&szText)
{
	int nID=0;
	memcpy(&nID,buff,sizeof(int));
	if(nID<0)
		return -1;
	if(nID==0)
	{
		sprintf(szText,"独立运行");
		return nID;
	}
	char sql[MAX_SQL_LENGTH];memset(sql,0x0,MAX_SQL_LENGTH);
	char *sql_buff=NULL;
	int len=0;
	switch(nTableID) 
	{
	case ID_TABLE_NODE_INFO:
		sprintf(sql, "select Name from NODE_INFO where ID=%d", nID);
		break;

	case ID_TABLE_NET_SERVER_UNIT:
		sprintf(sql, "select Name from NET_SERVER_UNIT where ID=%d",nID);
		break;
	case ID_TABLE_DB_SERVER_UNIT:
		sprintf(sql,"select Name from DB_SERVER_UNIT where ID=%d",nID);
		break;
	case ID_TABLE_WORKSTATION_UNIT:
		sprintf(sql,"select Name from WORKESTATION_UNIT where ID=%d",nID);
		break;
	case ID_TABLE_FRONT_TABLE:
		sprintf(sql,"select Name from FRONT_TABLE where ID=%d",nID);
		break;
	case ID_TABLE_FRONTROUT_TABLE:
		sprintf(sql,"select Name from FRONTROUT_TABLE where ID=%d",nID);
		break;
	}
	if (pDataBase)
	{
		if(pDataBase->DB_Select(m_Sys_DB.GetActiveNSA3000N_DB(),sql,sql_buff,&len)<=0)
			return 0;
		// 		if(pDataBase->GetCurDBTableRowCount(SYSTEM_TYPE_NSA_RD)<=0)
		// 			return 0;
		memcpy(szText,sql_buff,MAX_TEXT_LENGTH*sizeof(char));
		if(sql_buff)
		{
			delete sql_buff;
			sql_buff=NULL;
		}
	}
	// 	if(pRealDB)
	// 	{
	// 		pRealDB->RealDB_SQL(sql,sql_buff,&len);
	// 		if(len<=0)
	// 			return 0;
	// 		memcpy(szText,sql_buff,MAX_TEXT_LENGTH*sizeof(char));
	// 		if(sql_buff)
	// 		{
	// 			delete sql_buff;
	// 			sql_buff=NULL;
	// 		}
	// 	}
	return 0;	
}

INT CDBText::GetYXYCName(CNSA3000DB_Interface *pDataBase,char *buff,int nFieldIndex,char *&szText)
{
	char sql[MAX_SQL_LENGTH];memset(sql,0x0,MAX_SQL_LENGTH);
	char *sqlbuff=NULL;int buff_len=0;
	int nYXYCID=0;memcpy(&nYXYCID,buff,sizeof(int));
	if(nYXYCID<-1)
		return -1;
	if(nYXYCID==-1)
	{
		sprintf(szText,"无");
		return 1;
	}
	int nTableID=nYXYCID/SYSTEM_MAX_NUM;
	nYXYCID=nYXYCID%SYSTEM_MAX_NUM;
	char szTableName[32];memset(szTableName,0x0,32);
	GetEngName(pDataBase,nTableID,SYSTEM_TYPE_NSA_RD,szTableName);
	sprintf(sql,"select Name from %s where ID=%d",szTableName,nYXYCID);
	if(pDataBase)
	{
		if(pDataBase->DB_Select(m_Sys_DB.GetActiveNSA3000N_DB(),sql,sqlbuff,&buff_len)>0)
		{
			strcpy(szText,sqlbuff);
			delete sqlbuff;sqlbuff=NULL;
			return 1;
		}else
		{
			return -1;
		}
	}
	// 	if(pRealDB)
	// 	{
	// 		pRealDB->RealDB_SQL(sql,sqlbuff,&buff_len);
	// 		if(buff_len<=0)
	// 			return -1;
	// 		strcpy(szText,sqlbuff);
	// 		if(sqlbuff)
	// 		{
	// 			delete sqlbuff;
	// 			sqlbuff=NULL;
	// 		}
	// 	}
	return 1;
}
INT CDBText::GetEngName(CNSA3000DB_Interface *pDataBase,int table_no,int app_no,char *eng_name)
{
	if(pDataBase)
	{
		//		if(!pDataBase->is_connected)
		//		{
		//			AfxMessageBox("数据库没有连接,请检查配置!");
		//			return 0;
		//		}
		char sql[MAX_SQL_LENGTH];
		char *buff=NULL;
		int buff_len=0;
		memset(sql,0,MAX_SQL_LENGTH*sizeof(char));
		sprintf(sql,"select TABLE_NAME_ENG,MAX_RECORD_NUM from TABLE_INFO where TABLE_ID=%d and APP_TYPE=%d",table_no,app_no);
		if(pDataBase->DB_Select(m_Sys_DB.GetActiveNSA3000N_DB(),sql,buff,&buff_len)<0)
		{
			AfxMessageBox("获得表英文名失败！");
			return -1;
		}else
		{
			//	eng_name=new char[32];
			if(buff_len>0)
			{
				memset(eng_name,0,32);
				strcpy(eng_name,buff);
				memcpy(eng_name,buff,32);
			}
		}
		if(buff)
		{
			delete buff;
			buff=NULL;
		}
	}
	// 	if(pRealDB)
	// 	{
	// 		char *pTemp=NULL;
	// 		char sql[MAX_SQL_LENGTH];
	// 		memset(sql,0,MAX_SQL_LENGTH);
	// 		sprintf(sql,"select * from TABLE_INFO where TABLE_ID=%d and APP_TYPE=%d",table_no,app_no);
	// 		int len=0;
	// 		pRealDB->RealDB_SQL(sql,pTemp,&len);
	// 		if(len<32)
	// 			return -1;
	// 		memcpy(eng_name,pTemp+sizeof(INT),32*sizeof(CHAR));
	// 		if(pTemp)
	// 		{
	// 			delete pTemp;
	// 			pTemp=NULL;
	// 		}
	// 	}
	return 1;
}

INT CDBText::GetDataSourceText(CNSA3000DB_Interface *pDataBase,char *buff,char *&szText)
{
	int nID=0;
	memcpy(&nID,buff,sizeof(int));
	if(nID<0)
		return -1;
	if(nID==0)
	{
		sprintf(szText,"无");
		return 1;
	}
	char sql[MAX_SQL_LENGTH];memset(sql,0x0,MAX_SQL_LENGTH);
	char *sqlbuff=NULL;int buff_len=0;
	sprintf(sql,"select Name from NET_SERVER_UNIT where ID=%d",nID);
	if(pDataBase)
	{
		if(pDataBase->DB_Select(m_Sys_DB.GetActiveNSA3000N_DB(),sql,sqlbuff,&buff_len)>0)
		{
			strcpy(szText,sqlbuff);
			delete sqlbuff;sqlbuff=NULL;
			return 1;
		}else
			return -1;
	}
	// 	if(pRealDB)
	// 	{
	// 		pRealDB->RealDB_SQL(sql,sqlbuff,&buff_len);
	// 		if(buff_len<=0)
	// 			return -1;
	// 		strcpy(szText,sqlbuff);
	// 		if(sqlbuff)
	// 		{
	// 			delete sqlbuff;
	// 			sqlbuff=NULL;
	// 		}
	// 	}
	return 1;
}

INT CDBText::GetHexFromDecimal(DWORD nDecVal,char *buff)
{
	if (!buff )
		return -1;

	UCHAR TempVal = 0;
	DWORD temp1 = 1;
	DWORD temp2 = 0;

	buff[4] =0;
	for (int i=0; i<4; i++)
	{
		if (i==0)
		{
			temp1 = 1;
		}else
		{
			temp1 = temp1*0x10;
		}
		temp2 = temp1*0x10;
		if (temp1>0 && temp2>0)
		{
			TempVal = nDecVal%temp2/temp1;
			if (TempVal >= 0xa)
			{
				TempVal = TempVal+0x37;
			}
			else
			{
				TempVal = TempVal+0x30;
			}
			buff[3-i] = TempVal;
		}
	}

	return 1;
}

*/
